/*
 * Copyright 2019 Zhang XueHai
 * you may not use this file except in compliance with the License.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhangxuehai.android.pdfviewer.annotation.pen;

import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.zhangxuehai.android.pdfviewer.CoordinateUtils;
import com.zhangxuehai.android.pdfviewer.PDFView;
import com.shockwave.pdfium.util.SizeF;
import java.util.List;

/**
 * 可选颜色的笔，可以带透明度
 * @author 章学海
 * @date 2019/12/26
 */
class ColorPen implements Pen.WritePen {
    private transient Paint paint;
    /**画笔颜色*/
    private int color;
    /**画笔宽度比例*/
    private float penWidthScale;


    public ColorPen(int color, float penWidthScale) {
        this.color = color;
        this.penWidthScale = penWidthScale;
        init();
    }
    @Override
    public void init(){
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStrokeCap(Paint.Cap.SQUARE);
    }

    @Override
    public void draw(List<SizeF> points, Canvas canvas,float scale,int basePenWidth, PDFView pdfView,int page) {
        paint.setStrokeWidth(basePenWidth*penWidthScale/scale);
        paint.setPathEffect(new CornerPathEffect(paint.getStrokeWidth()));
        Path path = new Path();
        float x = points.get(0).getWidth();
        float y = points.get(0).getHeight();
        Point point = CoordinateUtils.toPdfPointCoordinateDesc(pdfView,page,x,y);
        path.moveTo(point.x/scale,point.y/scale);
        for (int i = 1; i < points.size(); i++) {
            x = points.get(i).getWidth();
            y = points.get(i).getHeight();
            point = CoordinateUtils.toPdfPointCoordinateDesc(pdfView,page,x,y);
            path.lineTo(point.x/scale,point.y/scale);
        }
        canvas.drawPath(path,paint);
    }

    @Override
    public PenType getPenType() {
        return PenType.COLOR;
    }
}